from flask import Flask, render_template, request, jsonify
import requests

app = Flask(__name__)
OLLAMA_URL = "http://localhost:11434/api/generate"

@app.route('/')
def index():
    return render_template('chat.html')

@app.route('/chat', methods=['POST'])
def chat():
    user_message = request.json['message']
    history = request.json.get('history', [])
    
    # 构建ollama请求数据
    data = {
        "model": "deepseek-r1:14b",
        "prompt": user_message,
        "stream": False,
        "context": history  # 传递对话上下文
    }
    
    try:
        response = requests.post(OLLAMA_URL, json=data)
        response.raise_for_status()
        result = response.json()
        return jsonify({
            'response': result['response'],
            'context': result.get('context', [])
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 500

if __name__ == '__main__':
    app.run(debug=True, port=5000) 